package com.example.webdemo.dao.Impl;

import com.example.webdemo.bean.base_dict;
import com.example.webdemo.dao.BaseDictDao;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.time.LocalDateTime;
import java.util.List;

@Repository
public class BaseDictDaoImpl implements BaseDictDao {
    private final JdbcTemplate jdbcTemplate;

    public BaseDictDaoImpl(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    // 自定义RowMapper处理LocalDateTime字段
    private final RowMapper<base_dict> rowMapper = (rs, rowNum) -> {
        base_dict dict = new base_dict();
        dict.setDict_id(rs.getInt("dict_id"));
        dict.setDict_type(rs.getString("dict_type"));
        dict.setDict_code(rs.getString("dict_code"));
        dict.setDict_name(rs.getString("dict_name"));
        dict.setDict_value(rs.getString("dict_value"));
        dict.setSort(rs.getInt("sort"));
        dict.setStatus(rs.getInt("status"));
        dict.setCreate_time(rs.getDate("create_time"));
        dict.setUpdate_time(rs.getDate("update_time"));
        return dict;
    };

    @Override
    public int addDict(base_dict dict) {
        String sql = "INSERT INTO base_dict (dict_type, dict_code, dict_name, dict_value, sort, status, create_time, update_time) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        LocalDateTime now = LocalDateTime.now();
        return jdbcTemplate.update(sql,
                dict.getDict_type(),
                dict.getDict_code(),
                dict.getDict_name(),
                dict.getDict_value(),
                dict.getSort(),
                dict.getStatus(),
                now,
                now);
    }

    @Override
    public int deleteDict(Integer dictId) {
        String sql = "DELETE FROM base_dict WHERE dict_id = ?";
        return jdbcTemplate.update(sql, dictId);
    }

    @Override
    public int updateDict(base_dict dict) {
        String sql = "UPDATE base_dict SET dict_type=?, dict_code=?, dict_name=?, dict_value=?, " +
                "sort=?, status=?, update_time=? WHERE dict_id=?";
        return jdbcTemplate.update(sql,
                dict.getDict_type(),
                dict.getDict_code(),
                dict.getDict_name(),
                dict.getDict_value(),
                dict.getSort(),
                dict.getStatus(),
                LocalDateTime.now(),
                dict.getDict_id());
    }

    @Override
    public base_dict getDictById(Integer dictId) {
        String sql = "SELECT * FROM base_dict WHERE dict_id = ?";
        return jdbcTemplate.queryForObject(sql, rowMapper, dictId);
    }

    @Override
    public List<base_dict> getAllDicts() {
        String sql = "SELECT * FROM base_dict ORDER BY sort";
        return jdbcTemplate.query(sql, rowMapper);
    }

    @Override
    public List<base_dict> getDictsByType(String dictType) {
        String sql = "SELECT * FROM base_dict WHERE dict_type = ? ORDER BY sort";
        return jdbcTemplate.query(sql, rowMapper, dictType);
    }

    @Override
    public List<base_dict> getDictsByTypeAndCode(String dictType, String dictCode) {
        String sql = "SELECT * FROM base_dict WHERE dict_type = ? AND dict_code = ?";
        return jdbcTemplate.query(sql, rowMapper, dictType, dictCode);
    }
}